﻿using PPSMS_Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPSMS_DAL;

public class InverterDevices_DAL : ICRUD_DAL<InverterDevices, long>
{
    /// <summary>
    /// 删除指定ID的记录
    /// </summary>
    /// <param name="ids">逆变器设备表id数组</param>
    public void DeleteByIds(params long[] ids)
    {
        string sql = $"delete from INVERTER_DEVICES where INVERTERID in ({string.Join(",", ids)})";
        DBHelper.ExecuteNonQuery(sql);
    }

    /// <summary>
    /// 获取当前最大ID值
    /// </summary>
    /// <returns>ID</returns>
    public long GetCurrentValue()
    {
        string sql = "select rowid from INVERTER_DEVICES";
        return DBHelper.ExecuteQuery(sql, DBHelper.ExecuteScalarToInt, new object[0]);
    }

    /// <summary>
    /// 新增记录
    /// </summary>
    /// <param name="t">逆变器设备表对象</param>
    public void Insert(InverterDevices t)
    {
        string sql = "insert into INVERTER_DEVICES (model, manufacturer,Rated_AC_Power,Module_Peak_Power) values(?,?,?,?)";
        DBHelper.ExecuteNonQuery(sql, t.Model, t.Manufacturer,t.RatedPower,t.Module_Peak_Power);
    }

    /// <summary>
    /// 将一个 DataRow 对象转换为对应的 InverterDevices 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>逆变器设备表对象</returns>
    public InverterDevices ResultHandler(DataRow dr)
    {
        return new InverterDevices()
        {
            InverterID = dr.Field<long>("INVERTERID"),
            Model = dr.Field<string>("MODEL"),
            Manufacturer = dr.Field<string>("MANUFACTURER"),
            RatedPower = dr.Field<string>("RATED_AC_POWER"),
            Module_Peak_Power=dr.Field<string>("MODULE_PEAK_POWER"),
        };
    }

    /// <summary>
    /// 将一个 DataTable 对象转换为对应的 List<InverterDevices> 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>一个包含所有逆变器设备表信息的List</returns>
    public List<InverterDevices> ResultHandlerList(DataTable dr)
    {
        List<InverterDevices> list = new List<InverterDevices>();
        foreach (DataRow item in dr.Rows)
        {
            list.Add(ResultHandler(item));
        }
        return list;
    }

    /// <summary>
    /// 获取所有逆变器设备表的记录
    /// </summary>
    /// <returns>一个包含所有逆变器设备表信息的List<InverterDevices></returns>
    public List<InverterDevices> SelectAll()
    {
        string sql = "select * from INVERTER_DEVICES";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }

    /// <summary>
    /// 根据ID获取逆变器设备表记录
    /// </summary>
    /// <param name="id">ID</param>
    /// <returns>逆变器设备表对象</returns>
    public InverterDevices? SelectById(long id)
    {
        string sql = "select * from INVERTER_DEVICES";
        return SelectAll().FirstOrDefault(x => x.InverterID == id);
    }

    /// <summary>
    /// 根据ID修改逆变器设备表记录
    /// </summary>
    /// <param name="t">新的逆变器设备表对象</param>
    /// <param name="id">旧的ID</param>
    /// <exception cref="InvalidOperationException"></exception>
    public void UpdateById(InverterDevices t, long id)
    {
        string sql = "update INVERTER_DEVICES set ";
        var list = new List<object>();
        if (!string.IsNullOrEmpty(t.Model))
        {
            sql += "MODEL = ?, ";
            list.Add(t.Model);
        }
        if (!string.IsNullOrEmpty(t.Manufacturer))
        {  
            sql += "MANUFACTURER = ?,";
            list.Add(t.Manufacturer);
        }
        sql = sql.TrimEnd(',');
        if(list.Count == 0) throw  new InvalidOperationException("没有需要更新的字段");
        sql += " where INVERTERID = ?";
        list.Add(id);
        DBHelper.ExecuteNonQuery(sql, list.ToArray());        
    }
}
